#include "DGGSPolygon.hpp"
#include <vector>
using namespace NNU::DGGS::Base;
using namespace NNU::DGGS::Feature;



DGGSPolygon *DGGSPolygon::deepCopy() {
    auto *newPolygon= new DGGSPolygon();

    newPolygon->_gridCount=this->_gridCount;
    newPolygon->setGridData(this->_gridData);

    return newPolygon;
}

bool DGGSPolygon::Equals(DGGSGeometry *other) {
    if(other->getShapeType()!=ST_Polygon)
        return false;
    else
    {
        DGGSPolygon *oPolygon = dynamic_cast<DGGSPolygon *>(other);

        if(this->_gridCount!=oPolygon->_gridCount)
            return false;

        //比较两要素是否相等
        DGGSElementCode* otherData= nullptr;
        other->getGridData(otherData);
        DGGSElementCode* srcData= nullptr;
        this->getGridData(srcData);

        if(otherData== nullptr||srcData== nullptr)
            return false;

        for(int i=0;i<this->_gridCount;i++)
        {
            if (srcData[i] == otherData[i])
                continue;
            else
                return false;
        }
        return true;
    }
}

bool DGGSPolygon::setGridData(DGGSElementCode *srcData) {
    //设置data
    if(srcData== nullptr)
        return false;
    this->_gridData=new DGGSElementCode[this->_gridCount];
    for(int i=0;i<this->_gridCount;i++)
        this->_gridData[i]=srcData[i];
    return true;
}

DGGSElementCode DGGSPolygon::getGridCodeByIndex(int index) {
    if (this->_gridCount == 0 || index > this->_gridCount || index < 0)
    {
        DGGSElementCode code;
        code.level = -1;
        return code;
    }
    else
        return this->_gridData[index];
}

void DGGSPolygon::setGridCount(int gridCount) {
    this->_gridCount=gridCount;
    if(this->_gridData== nullptr)
        this->_gridData = new DGGSElementCode[this->_gridCount];
}

bool DGGSPolygon::getGridData(DGGSElementCode* &srcData) {
    if(this->_gridCount==0)
        return false;
    srcData=new DGGSElementCode[this->_gridCount];
    for(int i=0;i<this->_gridCount;i++)
        srcData[i]=this->_gridData[i];
    return true;
}

void DGGSPolygon::addGridCode(DGGSElementCode srcCode) {
    //设置data
    for (int i = 0; i < this->_gridCount; i++)
        if (this->_gridData[i].level == -1)
            this->_gridData[i] = srcCode;
}

int DGGSPolygon::getGridCount() {
    return this->_gridCount;
}

DGGSPolygon::~DGGSPolygon() {
    if(this->_gridData!= nullptr)
        delete[]this->_gridData;
}

